package org.ezplatform.office.meeting.notice.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.meeting.info.entity.ApplyRecord;
import org.ezplatform.office.meeting.info.entity.MeetingScope;
import org.ezplatform.office.meeting.info.service.MeetingScopeService;
import org.ezplatform.office.meeting.notice.dao.NoticeTurnoverDao;
import org.ezplatform.office.meeting.notice.entity.NoticeTurnover;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class NoticeTurnoverService extends BaseService<NoticeTurnover, String> {

	@Autowired
	private NoticeTurnoverDao noticeTurnoverDao;
	@Lazy
	@Autowired
    private MeetingScopeService meetingScopeService;
	
	@Override
    protected JpaBaseDao<NoticeTurnover, String> getEntityDao() {
        return this.noticeTurnoverDao;
    }
	
	/**
	 * 会议转交，用户替换
	 * @param applyRecord
	 * @param token
	 * @param oldUserId 原用户
	 * @param turnoverUserId 转交用户
	 */
	public void changeUser(ApplyRecord applyRecord, String token, String turnoverUserId, String receiveUserId, String receiveUserName){
        Map<String, Object> map = new HashMap<String, Object>();
		//主持人
        List<String> eScopeList = new ArrayList<>();
        eScopeList.add(applyRecord.getEmceeUser());
        List<MeetingScope> emceeUserList = this.meetingScopeService.getMeetingScopeList("oa_meeting_apply_record", "emcee_user", eScopeList,true);
        //纪要员
        List<String> mScopeList = new ArrayList<>();
        mScopeList.add(applyRecord.getMemoUser());
        List<MeetingScope> memoUserList = this.meetingScopeService.getMeetingScopeList("oa_meeting_apply_record", "memo_user", mScopeList,true);
        //参会员
        List<String> aScopeList = new ArrayList<>();
        aScopeList.add(applyRecord.getAttendUser());
        List<MeetingScope> attendUserList = this.meetingScopeService.getMeetingScopeList("oa_meeting_apply_record", "attend_user", aScopeList,true);
        if(emceeUserList!=null && emceeUserList.size()>0){
            for(MeetingScope ms : emceeUserList){
               if(turnoverUserId.equals(ms.getScopevalue())) {
            	   this.meetingScopeService.updateScopeValue(receiveUserId, receiveUserName, ms.getId());
               }
            }
        }
        if(memoUserList!=null && memoUserList.size()>0){
            for(MeetingScope ms : memoUserList){
            	if(turnoverUserId.equals(ms.getScopevalue())) {
            		this.meetingScopeService.updateScopeValue(receiveUserId, receiveUserName, ms.getId());
                }
            }
        }
        if(attendUserList!=null && attendUserList.size()>0){
            for(MeetingScope ms : attendUserList){
                String scopeValue = ms.getScopevalue();
                String scopeType = ms.getScopetype();
                if("user".equals(scopeType) && turnoverUserId.equals(scopeValue)){
                	this.meetingScopeService.updateScopeValue(receiveUserId, receiveUserName, ms.getId());
                }
            }
        }
    }
	
	public void deleteByApplyrecordId(String... id) {
		this.noticeTurnoverDao.deleteByApplyrecordId(id);
	}
	
	public NoticeTurnover findByApplyrecordId(String applyRecordId, String turnoverUserId, String corpId) {
		return this.noticeTurnoverDao.findByApplyrecordId(applyRecordId, turnoverUserId, corpId);
	}
	
	public List<NoticeTurnover> findNoticeTurnoverList(String applyRecordId, String receiveUserId, String corpId){
		return this.noticeTurnoverDao.findNoticeTurnoverList(applyRecordId, receiveUserId, corpId);
	}
	
	public void deleteNoticeTurnover(String applyrecordId, String corpId) {
		this.noticeTurnoverDao.deleteNoticeTurnover(applyrecordId, corpId);
	}
}
